﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;

namespace BLL.Dal
{

    /// <summary>
    /// 数据访问类:tbUser
    /// </summary>
    public partial class SysUserDAL
    {
        public SysUserDAL()
        { }
        #region  BasicMethod

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(Guid ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tbUser");
            strSql.Append(" where ID=@ID ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@ID", OleDbType.Guid)			};
            parameters[0].Value = ID;

            return DbHelperOleDb.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.SysUser model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tbUser(");
            strSql.Append("ID,userName,userPwd,fullName,sex,birthday,roleCode)");
            strSql.Append(" values (");
            strSql.Append("@ID,@userName,@userPwd,@fullName,@sex,@birthday,@roleCode)");
            OleDbParameter[] parameters = {
					new OleDbParameter("@ID", OleDbType.Guid),
					new OleDbParameter("@userName", OleDbType.VarChar,255),
					new OleDbParameter("@userPwd", OleDbType.VarChar,255),
					new OleDbParameter("@fullName", OleDbType.VarChar,255),
					new OleDbParameter("@sex", OleDbType.VarChar,255),
					new OleDbParameter("@birthday", OleDbType.Date),
					new OleDbParameter("@roleCode", OleDbType.VarChar,255)};
            parameters[0].Value = Guid.NewGuid();
            parameters[1].Value = model.userName;
            parameters[2].Value = model.userPwd;
            parameters[3].Value = model.fullName;
            parameters[4].Value = model.sex;
            parameters[5].Value = model.birthday;
            parameters[6].Value = model.roleCode;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.SysUser model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbUser set ");
            strSql.Append("userName=@userName,");
            strSql.Append("userPwd=@userPwd,");
            strSql.Append("fullName=@fullName,");
            strSql.Append("sex=@sex,");
            strSql.Append("birthday=@birthday,");
            strSql.Append("roleCode=@roleCode");
            strSql.Append(" where ID=@ID ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@userName", OleDbType.VarChar,255),
					new OleDbParameter("@userPwd", OleDbType.VarChar,255),
					new OleDbParameter("@fullName", OleDbType.VarChar,255),
					new OleDbParameter("@sex", OleDbType.VarChar,255),
					new OleDbParameter("@birthday", OleDbType.Date),
					new OleDbParameter("@roleCode", OleDbType.VarChar,255),
					new OleDbParameter("@ID", OleDbType.Guid)};
            parameters[0].Value = model.userName;
            parameters[1].Value = model.userPwd;
            parameters[2].Value = model.fullName;
            parameters[3].Value = model.sex;
            parameters[4].Value = model.birthday;
            parameters[5].Value = model.roleCode;
            parameters[6].Value = model.ID;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(Guid ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbUser ");
            strSql.Append(" where ID=@ID ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@ID", OleDbType.Guid)			};
            parameters[0].Value = ID;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbUser ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.SysUser GetModel(Guid ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,userName,userPwd,fullName,sex,birthday,roleCode from tbUser ");
            strSql.Append(" where ID=@ID ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@ID", OleDbType.Guid)			};
            parameters[0].Value = ID;

            Model.SysUser model = new Model.SysUser();
            DataSet ds = DbHelperOleDb.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.SysUser DataRowToModel(DataRow row)
        {
            Model.SysUser model = new Model.SysUser();
            if (row != null)
            {
                if (row["ID"] != null && row["ID"].ToString() != "")
                {
                    model.ID = new Guid(row["ID"].ToString());
                }
                if (row["userName"] != null)
                {
                    model.userName = row["userName"].ToString();
                }
                if (row["userPwd"] != null)
                {
                    model.userPwd = row["userPwd"].ToString();
                }
                if (row["fullName"] != null)
                {
                    model.fullName = row["fullName"].ToString();
                }
                if (row["sex"] != null)
                {
                    model.sex = row["sex"].ToString();
                }
                if (row["birthday"] != null && row["birthday"].ToString() != "")
                {
                    model.birthday = DateTime.Parse(row["birthday"].ToString());
                }
                if (row["roleCode"] != null)
                {
                    model.roleCode = row["roleCode"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,userName,userPwd,fullName,sex,birthday,roleCode ");
            strSql.Append(" FROM tbUser ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperOleDb.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tbUser ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperOleDb.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            int pageSize = endIndex - startIndex;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,userName,fullName,sex,birthday,roleCode,'0' as temp_text from ( ");


            strSql.Append(" select top ");
            ///页大小
            strSql.Append(pageSize);
            strSql.Append(" * from ( ");

            #region
            strSql.Append(" select top ");
            strSql.Append(endIndex);
            strSql.Append(" * from tbUser ");
            strSql.Append(strWhere);
            strSql.Append("order by createDateTime desc");
            #endregion

            strSql.Append(")");
            strSql.Append("order by createDateTime asc");

            strSql.Append(")");
            strSql.Append("order by createDateTime desc");

            return DbHelperOleDb.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            OleDbParameter[] parameters = {
                    new OleDbParameter("@tblName", OleDbType.VarChar, 255),
                    new OleDbParameter("@fldName", OleDbType.VarChar, 255),
                    new OleDbParameter("@PageSize", OleDbType.Integer),
                    new OleDbParameter("@PageIndex", OleDbType.Integer),
                    new OleDbParameter("@IsReCount", OleDbType.Boolean),
                    new OleDbParameter("@OrderType", OleDbType.Boolean),
                    new OleDbParameter("@strWhere", OleDbType.VarChar,1000),
                    };
            parameters[0].Value = "tbUser";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperOleDb.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}
